<?php
include_once("conn.php");
session_start();

function show()
{
	echo '<div class="heading">';
		echo '<div class="cate_link">';
			echo $_POST["newcategory"];
		echo '</div>';
		echo '<div class="controls">';
			echo '<img src="png/tiny_plus.png">';
			echo '<img src="png/tiny_gear.png">';
		echo '</div>';
	echo '</div>';
	
	$sql="insert into category(cate_name,name) values('".$_POST["newcategory"]."','".$_SESSION["current_user"]."')";
	mysql_query($sql);

}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mycss3.css" />
<meta charset="utf-8">
<script type="text/javascript" src="jquery-1.7.1.js"></script>
<script type="text/javascript">
$(function(){
	$("#btn1").click(function(){
		$("#Login").slideToggle("slow");
		$("#Login_go").slideToggle("slow");
	});
	$("#go_back").click(function(){
		$("#Login").slideToggle("slow");
		$("#Login_go").slideToggle("slow");
	});
	$("#login1").click(function(){
		$("#Login").slideToggle("fast");
		$("#Login_go").slideToggle("fast");
	});

	$("#bt_addCate").click(function(){
	
		var newCate=$("#newcategory").val();
		var result=$("#wrapper");
		$.post("addCate.php",{"newCate":newCate},function(json){
			var div="<div id='BlockCate"+json.cid+"'>"+
						"<div class='heading'>"+
							"<div class='cate_link' id='head_"+json.cid+"'>"+json.cname+"</div>"+
							"<div class='controls'>"+
								"<a href='#' onclick=show_add_link("+"'link_"+json.cid+"')"+">"+
									"<img src='png/tiny_plus.png'>"+
								"</a>"+
								"<a href='#' onclick=modify_cate('c_"+json.cid+"')>"+
									"<img src='png/tiny_gear.png'>"+
								"</a>"+
							"</div>"+
						"</div>"+
						
						"<div id='c_"+json.cid+"' class='cateModify'>"+
							"<input type='text' value='"+json.cname+"' id='modify_"+json.cid+"' />"+
							"<input type='hidden' value='"+json.cid+"' id='cid_"+json.cid+"' />"+
							"<input type='button' value='保存' onclick=saveCate('modify_"+json.cid+"','cid_"+"json.cid"+"','head_"+json.cid+"'); />"+
							"<input type='button' value='删除类' onclick=delCate('cid_"+json.cid+"','BlockCate"+json.cid+"'); />"+
						"</div>"+
						
						"<div id='link_"+json.cid+"' class='linkAdd'>"+
							"<form action='c_add_link.php' method='post'>"+
								"<input type='text' name='new_webname' id='new_webname_"+json.cid+"' placeholder='new webname' />"+
								"<input type='text' name='new_webaddress' id='new_webaddress_"+json.cid+"' placeholder='new webaddress' />"+
								"<input type='hidden' name='hid_cid' id='hid_cid_"+json.cid+"' value='"+json.cid+"' />"+
								"<input type='button' id='bt_add_link' value='添加' onclick=addLink('new_webname_"+json.cid+"','new_webaddress_"+json.cid+"','c_link_"+json.cid+"','hid_cid_"+json.cid+"'); />"+
							"</form>"+
						"</div>"+
						
					"</div>";
			
			result.append(div);
		},'json');

	});
	
	var isDoingLogin = false;
	$("#bt_login").click(function(){
		var name1=$("#username").val();
		var pass1=$("#password").val();

		if (isDoingLogin)
		{
			return;
		}
		isDoingLogin = true;

		$.ajax({
			type:"POST",
			url:"login_process.php",
			dataType:"json",
			data:{"name2":name1,"pass2":pass1},
			beforeSend:function(){
				var div1="<p>正在登录中，请稍候...</p>";
				$("#Login_go").append(div1);
			},
			success:function(json){
				$("#b").remove();
				var div2="<li style='float:right;padding:0 3px;'>"+"<a href='manage.php'>"+"欢迎回来"+json.u1+"</a></li>";
				$("#a").append(div2);
			},
			error: function() {
				isDoingLogin = false;
			}

		});
	});

});
</script>


<script type="text/javascript">
function delLink(b,c,d,e)
{
	var webname=$("#"+b).val();
	var webaddress=$("#"+c).val();
	var delLink=$("#"+d);
	var obj=$("#"+e);
	$.post("delLink.php",{"webname":webname,"webaddress":webaddress},function(){
			delLink.remove();
			obj.remove();
	});
}

function saveLink(a,b,c,d,e) <!--a,b为修改栏的值,e为原来的webname,c,d为要更新的div id    -->
{
	var webname=$("#"+a).val();
	var webaddress=$("#"+b).val();
	var oldwebname=$("#"+e).val();
	
	var nameid=$("#"+c);
	var addressid=$("#"+d);

	$.post("saveLink.php",{"webname":webname,"webaddress":webaddress,"oldwebname":oldwebname},function(){
		nameid.html(webname);
		addressid.html(webaddress);
	});

}
function addLink(a,b,c,d)  <!-- a,b为欲增的名、址，c为过后要放地方的ID  d:为cid 要加入cate_link-->
{
	var webname=$("#"+a).val();
	var webaddress=$("#"+b).val();
	var cid=$("#"+d).val();
	var div=$("#"+c);


	$.post("addLink.php",{"webname":webname,"webaddress":webaddress,"cid":cid},function(json){
		var add="<div class='link' id='l1_"+json.a+"'>"+
				"<div class='link_name' id='l2_"+json.a+"'>"+
				"<div class='title' id='t_"+json.a+"'>"+json.webname11+"</div>"+
				"<div class='webaddress' id='a_"+json.a+"'>"+json.webaddress11+"</div></div>"+
				"<div class='controls'>"+"<a href='#' onclick=changeLink('l_"+json.a+"');>"+
				"<img src='png/tiny_gear.png'></a></div></div>";

		div.append(add);
	}, 'json');

}

function saveCate(a,b,c) <!--a为新类名b为原始类名 c为类名放置的位置-->
{
	var newcateName=$("#"+a).val();
	var oldcateCid=$("#"+b).val();
	var catePlace=$("#"+c);
	$.post("saveCate.php",{"newcateName":newcateName,"oldcateCid":oldcateCid },function(result){
		catePlace.html(result);
	});

}

function delCate(a,b)  <!--a为cid b为将要remove()的类块 -->
{
	var cid=$("#"+a).val();
	var block=$("#"+b);

	$.post("delCate.php",{"cid":cid},function(){
		block.remove();	
	});

}
/*
function addCate()
{
	var newCate=$("#newcategory").val();
	var result=$("#wrapper");
	$.post("addCate.php",{"newCate":newCate},function(json){
		var div="<div id='BlockCate"+json.cid+"'>"+
					"<div class='heading'>"+
						"<div class='cate_link' id='head_"+json.cid+"'>"+json.cname+"</div>"+
						"<div class='controls'>"+
							"<a href='#' onclick='show_add_link("+"'link_"+json.cid+"')"+">"+
								"<img src='png/tiny_plus.png'>"+
							"</a>"+
							"<a href='#' onclick='modify_cate('c_"+json.cid+"')>"+
								"<img src='png/tiny_gear.png'>"+
							"</a>"+
						"</div>"+
					"</div>"+
					
					"<div id='c_"+json.cid+"' class='cateModify'>"+
						"<input type='text' value='"+json.cname+"' id='modify_"+json.cid"' />"+
						"<input type='hidden' value='"+json.cid+"' id='cid_"+json.cid+"' />"+
						"<input type='button' value='保存' onclick=saveCate('modify_"+json.cid+"','cid_"+"json.cid"+"','head_"+json.cid+"'); />"+
						"<input type='button' value='删除类' onclick=delCate('cid_"+json.cid+"','BlockCate"+json.cid+"'); />"+
					"</div>"+
					
					"<div id='link_"+json.cid+"' class='linkAdd'>"+
						"<form action='c_add_link.php' method='post'>"+
							"<input type='text' name='new_webname' id='new_webname_"+json.cid+"' placeholder='new webname' />"+
							"<input type='text' name='new_webaddress' id='new_webaddress_"+json.cid+"' placeholder='new webaddress' />"+
							"<input type='hidden' name='hid_cid' id='hid_cid_"+json.cid+"' value='"+json.cid+"' />"+
							"<input type='button' id='bt_add_link' value='添加' onclick=addLink('new_webname_"+json.cid+"','new_webaddress_"+json.cid+"','c_link_"+json.cid+"','hid_cid_"+json.cid+"'); />"+
						"</form>"+
					
					"</div>"+
					
				"</div>";
		
		result.append(div);
	},'json');
}
*/

</script>

<script>
	function show_add_link(a)
	{
		var obj=document.getElementById(a);
		if(obj.style.display=="none")
		{
			obj.style.display='block';
		}
		else
		{
			obj.style.display='none';
		}
	}

	function modify_cate(a) 
	{
		var obj=document.getElementById(a);

		if(obj.style.display=="none")
		{
			obj.style.display='block';
		}
		else
		{
			obj.style.display='none';
		}

	}
	
	function changeLink(a)
	{
		var obj=document.getElementById(a);

		if(obj.style.display=="none")
		{
			obj.style.display='block';
		}
		else
		{
			obj.style.display='none';
		}

	}
</script>

</head>
<body>

<div id="Header">
	<ul id="a">
		<li><a href="index.php">首页</a></li>
		<li><a href="public.php">发现</a></li>
		<?php
		if($_SESSION["current_user"] == NULL )
		{
			echo '<li style="float:right;padding:0 3px;"><a href="reg.php">注册</a></li>';
			echo '<li id="b" style="float:right;padding:0 3px;"><a id="login1" href="#">登录</a></li>';
		}
		else
		{
			echo '<li style="float:right;padding:0 3px;"><a href="login_out.php">注销</a></li>';
			echo '<li style="float:right;padding:0 3px;"><a href="manage.php">'.$_SESSION["current_user"].'</a></li>';
		}
		?>
	</ul>

</div>

<div id="Login">
	<form>
		<input type="button" name="btn1" id="btn1" value="登录" />
	</form>
</div>

<div id="Login_go" style="display:none;">
	<form action="login_go.php" method="post" id="login_form">
		<tr>用户名<input type="text" name="username" id="username" /></tr>
		<tr>密码：<input type="password" name="password" id="password"/></tr>
		<tr><input type="button" value="登录" name="login" id="bt_login" /></tr>
		<tr><input type="reset" value="重置"  /></tr>
	</form>
	<tr><input type="button" id="go_back" value="返回" /></tr>
</div>

<div id="Content" style="background:#9BCD9B">
	<div id="Create">
		<form>
			<input type="text" class="cate" name="newcategory" id="newcategory" placeholder="input your new category name" />
			<input type="button" class="submit" value="Create" id="bt_addCate" name="Create" />
		</form>
	</div>
	
	<div id="wrapper">
		<?php
		if($_SESSION["current_user"] != NULL )
		{
			$query1 = "select cate_name,cid from category where name='".$_SESSION["current_user"]."'";
			$result = mysql_query($query1);
			while($gotCate = mysql_fetch_array($result) )
			{
				echo "<div id='BlockCate".$gotCate["cid"]."'>";
				echo '<div class="heading">';
					echo "<div class='cate_link' id='head_".$gotCate["cid"]."' >";
					echo $gotCate["cate_name"];
					echo '</div>';
					echo '<div class="controls">';
					echo "<a href='#' onclick=show_add_link('link_".$gotCate["cid"]."');>";
					echo '<img src="png/tiny_plus.png">';
					echo '</a>';
					echo "<a href='#' onclick=modify_cate('c_".$gotCate["cid"]."');>";
					echo '<img src="png/tiny_gear.png">';
					echo '</a>';
					echo '</div>';
				echo '</div>';
				
				/*下面这个是修改类名*/
				echo "<div id='c_".$gotCate["cid"]."' class='cateModify' style='display:none'; >";
				echo "<input type='text' value='".$gotCate["cate_name"]."' id='modify_".$gotCate["cid"]."' />";
				echo "<input type='hidden' value='".$gotCate["cid"]."' id='cid_".$gotCate["cid"]."' />";
				echo "<input type='button' value='保存' onclick=saveCate('modify_".$gotCate["cid"]."','cid_".$gotCate["cid"]."','head_".$gotCate["cid"]."'); />";
				echo "<input type='button' value='删除类' onclick=delCate('cid_".$gotCate["cid"]."','BlockCate".$gotCate["cid"]."') />";
				echo '</div>';
				/*上面这个是修改类名字*/
				/*下面这个是类里增加链接*/
				echo "<div id='link_".$gotCate["cid"]."' class='linkAdd' style='display:none';>";
				echo "<form action='c_add_link.php' method='post'>";
				echo "<input type='text' name='new_webname' id='new_webname_".$gotCate["cid"]."' placeholder='new webname' value='' />";echo "&nbsp&nbsp";
				echo "<input type='text' name='new_webaddress' id='new_webaddress_".$gotCate["cid"]."' placeholder='new webaddress' value='' />";echo "&nbsp&nbsp";
				echo "<input type='hidden' name='hid_cid' id='hid_cid_".$gotCate["cid"]."' value='".$gotCate["cid"]."' />";
				echo "<input type='button' id='bt_add_link' value='添加' onclick=addLink('new_webname_".$gotCate["cid"]."','new_webaddress_".$gotCate["cid"]."','c_link_".$gotCate["cid"]."','hid_cid_".$gotCate["cid"]."'); />";
				echo "</form>";
				echo "</div>";
				/*上面这个是类里增加链接*/
				$sql2="select category.cate_name,link.lid,webname,webaddress from category,cate_link,link where category.cid=cate_link.cid && link.lid=cate_link.lid ";
				$result2=mysql_query($sql2);
				echo "<div id='c_link_".$gotCate["cid"]."' class='link_form'>";
				while($gotLink = mysql_fetch_array($result2) )
				{
					
					if($gotCate["cate_name"] == $gotLink["cate_name"] )
					{
						//echo "<div id='all_".$gotCate["cid"]."'>";
						echo "<div class='link' id='l1_".$gotLink["lid"]."'>";
							echo "<div class='link_name' id='l2_".$gotLink["lid"]."'>";
							echo "<div class='title' id='t_".$gotLink["lid"]."' >";
							echo $gotLink["webname"];
							echo "</div>";
							echo "<div class='webaddress' id='a_".$gotLink["lid"]."' >";
							echo $gotLink["webaddress"];
							echo "</div>";
							echo '</div>';

							echo "<div class='controls'>";
							echo "<a href='#' onclick=changeLink('l_".$gotLink["lid"]."');><img src='png/tiny_gear.png'></a>";
							echo '</div>';
						echo '</div>';
						//echo "</div>";

						/*以下是修改LINK*/
						echo "<div class='linkModify' id='l_".$gotLink["lid"]."' style='display:none';>";
						echo "<input type='text' value='".$gotLink["webname"]."' id='newT_".$gotLink["lid"]."' />";
						echo "<input type='hidden' value='".$gotLink["webname"]."' id='title_".$gotLink["lid"]."' />";
						echo "<input type='hidden' value='".$gotLink["webaddress"]."' id='addr_".$gotLink["lid"]."' />";
						echo "<input type='text' value='".$gotLink["webaddress"]."' id='newA_".$gotLink["lid"]."' />";
						echo "<input type='button' value='保存' onclick=saveLink('newT_".$gotLink["lid"]."','newA_".$gotLink["lid"]."','t_".$gotLink["lid"]."','a_".$gotLink["lid"]."','title_".$gotLink["lid"]."') />";
						echo "<input type='button' value='删除' id='bt_".$gotLink["lid"]."' onclick=delLink('title_".$gotLink["lid"]."','addr_".$gotLink["lid"]."','l1_".$gotLink["lid"]."','l_".$gotLink["lid"]."') />";
						echo "</div>";
						/*以上是修改LINK*/
					}
					
						
					
				}
				echo "</div>";
				echo "</div>";
			}
			
		}
		?>
	</div>
	<!--
	<div class="receive_cate">
	<?php
	include_once("conn.php");
	
	if($_POST["newcategory"] != NULL && $_SESSION["current_user"]!=NULL )
	{
		show();
		echo "<meta http-equiv='refresh' content='0.5'; url='index.php' />";
	}
	
	?>
	</div>
	-->

</div>

<div id="Footer">
</div>


</body>
</html>